import Bus from '@/utils/bus.js';
import {
	getPiciNO
} from "@/utils/request.js";

export default {
	name: 'tags',
	data() {
		return {
			tagsList: [], //标签列表
		}
	},
	mounted() {
	},
	computed: {
        showTags() {
            return this.tagsList.length > 0;
        }
    },
	watch:{
        $route(newValue, oldValue){
        	this.setTags(newValue);
        	let tagsList = this.tagsList;
        	if(this.tagsList && this.tagsList.length > 0){
        		//只显示一个批次流程追溯表tag
        		let obj = this.tagsList.filter(res => res.path.indexOf("/flowTable") != "-1");
        		if(obj && obj.length > 1){
        			this.tagsList.map((res,index) => {
        				if(res.path.indexOf("/flowTable") != "-1"){
        					this.handleCloseTags(index);
        					//刷新批次流程追溯表界面
        					Bus.$emit('refreshFlowTable', 'true');
        				}
        			});
        		}
        	}
        }
    },
    created(){
        this.setTags(this.$route);
        // 监听关闭当前页面的标签页
        Bus.$on('close_current_tags', () => {
            for (let i = 0, len = this.tagsList.length; i < len; i++) {
                const item = this.tagsList[i];
                if(item.path === this.$route.fullPath){
                    if(i < len - 1){
                        this.$router.push(this.tagsList[i+1].path);
                    }else if(i > 0){
                        this.$router.push(this.tagsList[i-1].path);
                    }else{
                        this.$router.push('/');
                    }
                    this.tagsList.splice(i, 1);
                    break;
                }
            }
        })
    },
	components: {
//		Header: () => //头部
//			import('../../components/header/header.vue'),
//		Footer: () => //底部
//			import('../../components/footer/footer.vue'),
	},
	methods: {
		isActive(path) {
            return path === this.$route.fullPath;
        },
		// 设置标签
        setTags(route){
            const isExist = this.tagsList.some(item => {
                return item.path === route.fullPath;
            })
            if(!isExist){
                if(this.tagsList.length >= 8){
                	//把数组的第一个元素从其中删除，
                	//并返回第一个元素的值。
                    this.tagsList.shift();
                }
                this.tagsList.push({
                    title: route.meta.title,
                    path: route.fullPath,
                    name: route.matched[1].components.default.name
                })
            }
            Bus.$emit('tags', this.tagsList);
        },
        handleTags(command){
            command === 'other' ? this.handleCloseOther() : this.handleCloseAll();
        },
        //关闭标签
        handleCloseTags(index){
        	const delItem = this.tagsList.splice(index, 1)[0];
            const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1];
            if (item) {
                delItem.path === this.$route.fullPath && this.$router.push(item.path);
            }else{
                this.$router.push('/');
            }
        },
        //关闭其他
        handleCloseOther(){
        	const curItem = this.tagsList.filter(item => {
                return item.path === this.$route.fullPath;
            })
            this.tagsList = curItem;
        },
        //关闭所有
        handleCloseAll(){
        	this.tagsList = [];
            this.$router.push('/');
        },
	}
}